<script lang="ts" setup>
  import { computed, onBeforeMount, onBeforeUnmount, onMounted, reactive } from 'vue';
  import { useAppStore, DeviceType } from '/@/store/modules/app';
  import { useSettingsStore } from '/@/store/modules/settings';
  import { AppMain, NavigationBar, Sidebar, TagsView } from '/@/layouts/components';
  import useResize from './../useResize';

  const appStore = useAppStore();
  const settingsStore = useSettingsStore();
  const { sidebar, device, addEventListenerOnResize, resizeMounted, removeEventListenerResize, watchRouter } = useResize();

  const state = reactive({
    handleClickOutside: () => {
      appStore.closeSidebar(false);
    }
  });
  const classObj = computed(() => {
    return {
      hideSidebar: !sidebar.value.opened,
      openSidebar: sidebar.value.opened,
      withoutAnimation: sidebar.value.withoutAnimation,
      mobile: device.value === DeviceType.Mobile
    };
  });
  const showTagsView = computed(() => {
    return settingsStore.showTagsView;
  });
  const fixedHeader = computed(() => {
    return settingsStore.fixedHeader;
  });

  watchRouter();
  onBeforeMount(() => {
    addEventListenerOnResize();
  });
  onMounted(() => {
    resizeMounted();
  });
  onBeforeUnmount(() => {
    removeEventListenerResize();
  });
</script>

<template>
  <div :class="classObj" class="app-wrapper">
    <div v-if="classObj.mobile && sidebar.opened" class="drawer-bg" @click="state.handleClickOutside" />
    <Sidebar class="sidebar-container" />
    <div :class="{ hasTagsView: showTagsView }" class="main-container">
      <div v-if="fixedHeader" class="fixed-header-block"></div>
      <div :class="{ 'fixed-header': fixedHeader }">
        <NavigationBar />
        <TagsView v-if="showTagsView" />
      </div>
      <AppMain />
    </div>
  </div>
</template>

<style lang="scss" scoped>
  @import '/@/styles/mixins.scss';

  .app-wrapper {
    @include clearfix;

    position: relative;
    width: 100%;
  }

  .drawer-bg {
    position: absolute;
    top: 0;
    z-index: 999;
    width: 100%;
    height: 100%;
    background-color: #000;
    opacity: 0.3;
  }

  .main-container {
    position: relative;
    min-height: 100%;
    margin-left: var(--g-sidebar-width);
    transition: margin-left 0.28s;
  }

  .sidebar-container {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 1001;
    width: var(--g-sidebar-width) !important;
    height: 100%;
    overflow: hidden;
    font-size: 0;
    transition: width 0.28s;
  }

  .fixed-header {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 9;
    width: calc(100% - var(--g-sidebar-width));
    transition: width 0.28s;
  }

  .fixed-header-block {
    height: calc(var(--g-navigationbar-height) + var(--g-tagsview-height));
  }

  .hideSidebar {
    .main-container {
      margin-left: var(--g-sidebar-hide-width);
    }

    .sidebar-container {
      width: var(--g-sidebar-hide-width) !important;
    }

    .fixed-header {
      width: calc(100% - var(--g-sidebar-hide-width));
    }
  }

  // for mobile response 适配移动端
  .mobile {
    .main-container {
      margin-left: 0;
    }

    .sidebar-container {
      width: var(--g-sidebar-width) !important;
      transition: transform 0.28s;
    }

    &.openSidebar {
      position: fixed;
      top: 0;
    }

    &.hideSidebar {
      .sidebar-container {
        pointer-events: none;
        transform: translate3d(calc(0px - var(--g-sidebar-width)), 0, 0);
        transition-duration: 0.3s;
      }
    }

    .fixed-header {
      width: 100%;
    }
  }

  .withoutAnimation {
    .main-container,
    .sidebar-container {
      transition: none;
    }
  }
</style>
